import os
import random
import time
import threading
from multiprocessing import Process, Pool


def run_process(name):
    print("子进程是(%s),(%s)" % (name, os.getpid()))


# 创建子进程
def son_process():
    print("父进程是(%s)" % os.getpid())
    for i in range(5):
        p = Process(target=run_process, args=(str(i),))
        print("子进程(%s)开始" % str(i))
        p.start()
    p.join()


def run_task(name):
    print('任务是(%s),进程名是(%s)' % (name, os.getpid()))
    time.sleep(random.random() * 3)
    print("任务(%s)结束" % name)


# 创建进程池
def pool_demo():
    p = Pool(processes=3)
    for i in range(5):
        p.apply_async(run_task, (str(i),))
    print("等待任务结束")
    p.close()
    p.join()
    print("所有任务结束")


def run_thread(name):
    print('线程(%s)开始任务,线程名字是(%s)' % (name, threading.current_thread().name))
    time.sleep(random.random() * 3)


def thread_demo():
    print("当前线程是(%s)" % threading.current_thread().name)
    t = threading.Thread(target=run_thread, name="线程一", args=("线程1",))
    t.start()
    t.join()
    print("结束")
